<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>顶部导航栏.html</title>
    <script src="js/jquery-3.6.3.js"></script>
    <style type="text/css">
        * {
            margin: 0;
            padding: 0;
            list-style: none;
            box-sizing: border-box;
            text-decoration: none;
        }

        .clearfix::after {
            content: "";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }

        .advertisement {
            width: 100%;
            height: 30px;
            background-color: red;
        }

        .nav {
            width: 100%;
            height: 100px;
            background-color: greenyellow;
        }

        .main {
            width: 100%;
            height: 1000px;
            margin-top: 10px;
            background-color: darkgrey;
        }
    </style>
</head>
<body>
<div class="advertisement">

</div>
<div class="nav">
    <span>这是导航栏，滚动时我会吸顶的</span>
</div>
<div class="main"></div>
</body>

<script>
    $(function () {
        $(window).scroll(function () {
            let top = $(window).scrollTop()
            if (top >= 30) {
                $('.nav').css({
                    position: 'fixed',
                    top: 0,
                    left: 0
                })
                $('.main').css({
                    marginTop: 110,
                })
            } else {
                $('.nav').css({
                    position: 'static',
                })
                $('.main').css({
                    marginTop: 10,
                })
            }
        })
    })


</script>
</html>